<template>
    <div id='' class='user_content'>
        <i-table class="table_content" ref="myTable" :search="getSearch" :table="getTbale" :pageConfig="pageConfig" @resetTable="ResetTable" />
        <Modal v-model="modalShow" width="1200" :mask-closable="false" :title="modalTitle">
            <components v-if="modalShow" :is="comps" ref="coms" />
            <div slot="footer">
                <Button type="text" @click="cancel">取消</Button>
                <Button type="primary" @click="ok">确定</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import { config } from './user/table_config'
import addUpdata from "./user/add-updata.vue";
export default {
    name: '',
    props: [''],
    data() {
        return {
            search: {}, // 查询配置对象
            tableConfig: {}, // 表格配置对象
            pageConfig: config.page, // 翻页器配置
            comps: addUpdata,
            modalShow: false,
            modalTitle: '',
            ssbmList: []
        };
    },
    components: {},
    computed: {
        getSearch() {
            this.search = icu.util.deepClone(config.search);// 为不改变原始配置数据，进行深度克隆。
            for (let i = 0; i < this.search.info.length; i++) {
                const element = this.search.info[i];
                if (element.key == 'deptId') {
                    element.select = this.ssbmList // 根据字典项默认给选择框赋值。
                }
            }
            return this.search;
        },
        getTbale() {
            this.tableConfig = icu.util.deepClone(config.table); // 深度克隆表格配置。
            this.tableConfig.event = {
                edit: this.edit,
                add: this.add,
                reset: this.reset,
                remove: this.remove,
                updateGZL: this.updateGZL,
            }
            return this.tableConfig;
        }
    },
    beforeMount() { },
    mounted() {
        this['$api'] = this.$Api.get(['systemSetup:systemManger'])
        this.$http.$get('deptTopTree', { query: {} }).then(res => {
            this.ssbmList = this.setTreeData(this.moveChildrenLevel(res.data[0].children))
        })
    },
    methods: {
        updateGZL() {
            this.$http.$get('syncHorizonUse', { params: {} }).then(res => {
                console.log('===> res%o', res)
                if (res.code == 200) {
                    this.$Message.success(res.data);
                }
            })
        },
        add(row) {
            this.modalShow = true
            this.$nextTick(() => {
                this.$refs.coms.setPsdShow(true);
            })
            this.modalTitle = '新增'
        },
        edit(event, row) {
            this.modalShow = true
            this.modalTitle = '编辑'
            this.$nextTick(() => {
                this.$refs.coms.setPsdShow(false);
                this.$refs.coms.setData(row)
            })
        },
        reset(event, row) {
            this.$Modal.confirm({
                title: '提示信息',
                content: '密码将被重置为123456，是否确定重置？',
                onOk: () => {
                    this.$http.$get('resetPasswordpermissionUserManage', { params: { id: row.id } }).then(res => {
                        if (res.code == 200) {
                            this.$Message.success('重置成功');
                            this.cancel()
                            this.ResetTable()
                            return
                        }
                        this.$Message.error(res.msg);
                    })
                },
                onCancel: () => {
                    this.$Message.info('已取消');
                }
            });
        },
        ok() {
            let FormData = {};
            FormData = this.$refs.coms.getForm()
            if (!FormData) return
            this.$http.$post('SavepermissionUserManage', { data: FormData }).then(res => {
                if (res.code == 200) {
                    this.$Message.success('保存成功');
                    this.cancel()
                    this.ResetTable()
                } else {
                    if (res.msg == '账号已被使用') {
                        this.$Message.warning('用户名已存在，请重新输入!')
                    } else {
                        this.$Message.error(res.msg);
                    }
                }
            })
        },
        cancel() {
            this.modalShow = false
            this.modalTitle = ''
        },
        remove(event, row) {
            this.$Modal.confirm({
                title: '删除确认',
                content: '确认删除该条数据？',
                onOk: () => {
                    this.$http.$post('DeletepermissionUserManage', { data: [row.id] }).then(res => {
                        if (res.code == 200) {
                            this.$Message.success('删除成功');
                            this.ResetTable()
                            return
                        }
                        this.$Message.error(res.msg);
                    })
                },
            });
        },
        ResetTable() {
            this.$refs.myTable.seach()
        },
        moveChildrenLevel(arr1) {
            const send = []
            const move = arr2 => {
                arr2.forEach(item => {
                    send.push(item)
                    if (item.children) {
                        const children = Object.assign([], item.children)
                        delete item.children
                        move(children)
                    }
                })
            }
            move(arr1)
            return send
        },
        setTreeData(data) {
            let setdata = data.filter((item) => item.pid);
            for (let item of setdata) {
                item.label = item.title;
            }
            return data;
        },
    },
    watch: {}
}
</script>
<style lang='less' scoped>
/deep/.ivu-modal-body {
    height: 600px;
    overflow: auto;
}

.user_content {
    background-color: #fff;
    padding: 0 20px;
    width: 100%;
    height: 800px;

    .table_content {
        height: 752px;
        overflow: auto;
    }
}
</style>